package com.api.aiyh_guijiu.dao;

import aiyh.utils.Util;
import aiyh.utils.zwl.common.ToolUtil;
import com.api.aiyh_guijiu.pojo.UfWorkflowConflicDt1DTO;
import com.api.aiyh_guijiu.pojo.WorkflowBaseInfoDTO;
import com.api.aiyh_guijiu.pojo.WorkflowConfigDTO;
import com.api.aiyh_guijiu.vo.ConfigInfoVo;
import org.h2.util.StringUtils;
import weaver.conn.RecordSet;
import weaver.workflow.workflow.WorkflowVersion;

import java.util.ArrayList;
import java.util.List;

/**
 * @author EBU7-dev1-ayh
 * @create 2021/9/10 0010 11:27
 * 查询配置信息
 */


public class ConflictWorkflowDao {

	RecordSet rs = new RecordSet();
	ToolUtil toolUtil = new ToolUtil();

	public List<WorkflowConfigDTO> queryConfig(String workflowId) {

		String query = "select * from uf_workflow_conflic_dt1 " +
				"where workflow_type in (select id from workflow_base " +
				"where activeVersionID in (select activeVersionID " +
				"from workflow_base where id = ?) or id = ?)";
		rs.executeQuery(query, workflowId, workflowId);
		UfWorkflowConflicDt1DTO ufWorkflowConflicDt1DTO = Util.recordeSet2Entity(rs, UfWorkflowConflicDt1DTO.class, true);
		if (ufWorkflowConflicDt1DTO == null) {
			return null;
		}
//		String ids = WorkflowVersion.getVersionStringByWfid(workflowId);
		query = "select dt.id,dt.workflow_type,main.enable,wf.tablename,wf.fieldname  start_date_field, " +
				"wf1.fieldname  start_time_field,wf2.fieldname  end_date_field,wf4.fieldname  hrm_field, " +
				"wf3.fieldname  end_time_field from uf_workflow_conflic  main " +
				"left join uf_workflow_conflic_dt1  dt on dt.mainid = main.id  " +
				"left join workflow_field_table_view  wf on wf.id = dt.start_date_field " +
				"left join workflow_field_table_view  wf1 on wf1.id = dt.start_time_field " +
				"left join workflow_field_table_view  wf2 on wf2.id = dt.end_date_field " +
				"left join workflow_field_table_view  wf3 on wf3.id = dt.end_time_field " +
				"left join workflow_field_table_view  wf4 on wf4.id = dt.hrm_field " +
				"where main.id = ? and main.enable = 0";
//		and dt.workflow_type not in (" + ids + ")
		rs.executeQuery(query, ufWorkflowConflicDt1DTO.getMainid());
		return Util.recordeSet2Array(rs, WorkflowConfigDTO.class, true);
	}

	public List<WorkflowBaseInfoDTO> queryConflict(WorkflowConfigDTO workflowConfigDTO, String startTime
			, String startDate, String endTime, String endDate, String hrmId, String requestId) {
//	    (a,b)(c,d)  == > (2021-09-10 12:00:23 , 2021-09-10 20:00:23) (2021-09-10 15:00:23 , 2021-09-10 19:00:23)
//		select * from table where (hrm = ? or hrm like "%?%") and a <= d and b >= c and requestid != requestId;
		String query = "select id from " + workflowConfigDTO.getTablename() +
				" where (',' || " + workflowConfigDTO.getHrmField() +" || ',') like  ('%,' || '" + hrmId + "' || ',%')  " +
				" and requestid != " + requestId + " and " + workflowConfigDTO.getStartDateField() + " is not null " +
				" and " + workflowConfigDTO.getEndDateField() + " is not null" +
				(StringUtils.isNullOrEmpty(workflowConfigDTO.getStartTimeField()) ? "" : " and " + workflowConfigDTO.getStartTimeField() + " is not null") +
				(StringUtils.isNullOrEmpty(workflowConfigDTO.getEndTimeField()) ? "" : " and " + workflowConfigDTO.getEndTimeField() + " is not null");
		rs.executeQuery(query);
		List<String> integers = Util.recordeSet2Array(rs, String.class);
		String ids = String.join(",", integers);
		if(StringUtils.isNullOrEmpty(ids)){
			ids = "-1";
		}
		query = "select * from " +
				workflowConfigDTO.getTablename() +
				" where to_date(concat(" +
				workflowConfigDTO.getStartDateField() +
				" || ' ' ," +
				(StringUtils.isNullOrEmpty(workflowConfigDTO.getStartTimeField()) ? "'00:00:00'" : workflowConfigDTO.getStartTimeField()) +
				"), 'yyyy-mm-dd hh24:mi:ss') <=  to_date('" +
				endDate +
				" " +
				(StringUtils.isNullOrEmpty(endTime) ? "23:59:59" : endTime) +
				"', 'yyyy-mm-dd hh24:mi:ss')" +
				" and to_date(concat(" +
				workflowConfigDTO.getEndDateField() +
				" || ' ' ," +
				(StringUtils.isNullOrEmpty(workflowConfigDTO.getEndTimeField()) ? "'23:59:59'" : workflowConfigDTO.getEndTimeField()) +
				"), 'yyyy-mm-dd hh24:mi:ss') >= to_date('" +
				startDate +
				" " +
				(StringUtils.isNullOrEmpty(startTime) ? "00:00:00" : startTime) +
				"', 'yyyy-mm-dd hh24:mi:ss')  " +
				" and  id in (" + ids + ")";
		/*query = "select * from " +
				workflowConfigDTO.getTablename() +
				" where to_date(concat(" +
				workflowConfigDTO.getStartDateField() +
				" || ' ' ," +
				(StringUtils.isNullOrEmpty(workflowConfigDTO.getStartTimeField()) ? "'00:00:00'" : workflowConfigDTO.getStartTimeField()) +
				"), 'yyyy-mm-dd hh24:mi:ss') <=  to_date('" +
				endDate +
				" " +
				(StringUtils.isNullOrEmpty(endTime) ? "23:59:59" : endTime) +
				"', 'yyyy-mm-dd hh24:mi:ss')" +
				" and to_date(concat(" +
				workflowConfigDTO.getEndDateField() +
				" || ' ' ," +
				(StringUtils.isNullOrEmpty(workflowConfigDTO.getEndTimeField()) ? "'23:59:59'" : workflowConfigDTO.getEndTimeField()) +
				"), 'yyyy-mm-dd hh24:mi:ss') >= to_date('" +
				startDate +
				" " +
				(StringUtils.isNullOrEmpty(startTime) ? "00:00:00" : startTime) +
				"', 'yyyy-mm-dd hh24:mi:ss')  " +
				" and " + " (',' || " + workflowConfigDTO.getHrmField() + " || ',') like " +  " ('%,' || '" + hrmId + "' || ',%') "  +
				" and requestid != " + requestId;*/
				/*hrmId +
				" in (select regexp_substr(hrm_str, '[^,]+', 1, level) " +
				"from dual connect by regexp_substr(hrm_str, '[^,]+', 1, level) is not null)" +
				" and requestid != " + requestId;*/
//		toolUtil.writeErrorLog(query);

//		System.out.println(query);
		/*query = "select * from " + workflowConfigDTO.getTablename() + " where to_date(concat("
				+ workflowConfigDTO.getStartDateField() + " || ' ' ," + workflowConfigDTO.getStartTimeField()
				+ "), 'yyyy-mm-dd hh24:mi:ss') <=  to_date(concat("
				+ endDate + " " + endTime + "), 'yyyy-mm-dd hh24:mi:ss')"
				+ " and " + workflowConfigDTO.getEndDateField() + " || ' ' ," + workflowConfigDTO.getEndTimeField()
				+ "), 'yyyy-mm-dd hh24:mi:ss') >= to_date(concat("
				+ startDate + " " + startTime + "), 'yyyy-mm-dd hh24:mi:ss') " + hrmId
				+ " and in (select regexp_substr(hrm_str, '[^,]+', 1, level) " +
				"from dual connect by regexp_substr(hrm_str, '[^,]+', 1, level) is not null)";*/
		try {
			rs.executeQuery(query);
		}catch (Exception e){
			toolUtil.writeErrorLog("查询重复的流程错误，错误信息:" + e);
		}
		List<WorkflowBaseInfoDTO> list = new ArrayList<>();
		while(rs.next()){
//			存在时间段冲突的数据，查询冲突数据的流程信息
			RecordSet rsT = new RecordSet();
			requestId = Util.null2String(rs.getString("requestid"));
			query = "select distinct  wrb.requestid,wrb.workflowid,wrb.requestnamenew,wb.workflowname, " +
					"(case when hr.lastname is null and wrb.creater != 1 then '未知'  " +
					"when wrb.creater = 1 then '系统管理员' else hr.lastname  end)  creater,  " +
					"wrb.createdate,wrb.createtime from workflow_requestbase  wrb  " +
					"left join HrmResource  hr on wrb.creater = hr.id " +
					"left join workflow_base  wb on wb.id = wrb.workflowid " +
					"where wrb.requestid = ?";
			rsT.executeQuery(query, requestId);
			WorkflowBaseInfoDTO workflowBaseInfoDTO = Util.recordeSet2Entity(rsT, WorkflowBaseInfoDTO.class, true);
			list.add(workflowBaseInfoDTO);
		}
//		if (rs.next()) {
//			toolUtil.writeDebuggerLog("into next;");
////			存在时间段冲突的数据，查询冲突数据的流程信息
//			requestId = Util.null2String(rs.getString("requestid"));
//			query = "select wrb.workflowid,wrb.requestid,wrb.requestnamenew,wb.workflowname, " +
//					"(case when hr.lastname is null and wrb.creater != 1 then '未知'  " +
//					"when wrb.creater = 1 then '系统管理员' else hr.lastname  end)  creater,  " +
//					"wrb.createdate,wrb.createtime from workflow_requestbase  wrb  " +
//					"left join HrmResource  hr on wrb.creater = hr.id " +
//					"left join workflow_base  wb on wb.id = wrb.workflowid " +
//					"where wrb.requestid = ?";
//			rs.executeQuery(query, requestId);
//			return Util.recordeSet2Entity(rs, WorkflowBaseInfoDTO.class, true);
//		}
		if(list.size() == 0){
			return null;
		}
		return list;
	}

	public List<Integer> getWatermarkConfig(String workflowId) {
		String query = "select id from workflow_base " +
				"where activeVersionID in (select activeVersionID " +
				"from workflow_base where id = ?) or id = ?";
		rs.executeQuery(query, workflowId, workflowId);
		return Util.recordeSet2Array(rs, Integer.class);
	}

	public WorkflowConfigDTO getWorkflowConfig(String workflowId){
		String query = "select * from uf_workflow_conflic_dt1 " +
				"where workflow_type in (select id from workflow_base " +
				"where activeVersionID in (select activeVersionID " +
				"from workflow_base where id = ?) or id = ?)";
		rs.executeQuery(query, workflowId, workflowId);
		UfWorkflowConflicDt1DTO ufWorkflowConflicDt1DTO = Util.recordeSet2Entity(rs, UfWorkflowConflicDt1DTO.class, true);
		if (ufWorkflowConflicDt1DTO == null) {
			return null;
		}
		String ids = WorkflowVersion.getVersionStringByWfid(workflowId);
		query = "select dt.id,main.enable,wf.tablename,wf.fieldname  start_date_field, " +
				"wf1.fieldname  start_time_field,wf2.fieldname  end_date_field,wf4.fieldname  hrm_field, " +
				"wf3.fieldname  end_time_field from uf_workflow_conflic  main " +
				"left join uf_workflow_conflic_dt1  dt on dt.mainid = main.id  " +
				"left join workflow_field_table_view  wf on wf.id = dt.start_date_field " +
				"left join workflow_field_table_view  wf1 on wf1.id = dt.start_time_field " +
				"left join workflow_field_table_view  wf2 on wf2.id = dt.end_date_field " +
				"left join workflow_field_table_view  wf3 on wf3.id = dt.end_time_field " +
				"left join workflow_field_table_view  wf4 on wf4.id = dt.hrm_field " +
				"where main.id = ? and main.enable = 0 and dt.workflow_type in (" + ids + ")";
		rs.executeQuery(query, ufWorkflowConflicDt1DTO.getMainid());
		return Util.recordeSet2Entity(rs,WorkflowConfigDTO.class,true);
	}


	public ConfigInfoVo getConfigInfo(String tableName,String workflowId){
		String query = "select cf.workflow_type ,cf.workflow_node ,cf.enable ,wf.fieldname workflow_field  from " + tableName +
				" cf left join workflow_field_table_view  wf on wf.id = cf.workflow_field " +
				" where cf.workflow_type = ?";
		rs.executeQuery(query,workflowId);
		return Util.recordeSet2Entity(rs, ConfigInfoVo.class, true);
	}
}
